var database = require('../../../data/data.js');
Page({
  data: {
    flag: true,
    flag1: true,
    flag2: true,
    flag3: true,
    gameFlag: false
  },
  onLoad: function (options) {
    var all = 2;
    var count = 0;
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    this.setData({
      share_openid: options.popenid,
      share_id: options.gameid
    })
    var that = this;
    wx.request({
      url: database.url + '/project/getContestStatus.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        gameid: options.gameid
      },
      success(res) {
        if (res.data.result == -1) {
          that.setData({
            gameFlag: true
          })
        } else {
          that.setData({
            gameFlag: false
          })
        }
        wx.request({
          url: database.url1 + '/api/V2/user/access_token',
          data: {
            clientId: "QqKTO5BTPN6REI26QAgq",
            clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
          },
          success(res) {
            database.accessToken = res.data.data.accessToken;
            wx.request({
              url: database.url + '/project/getContestDetail.action',
              data: {
                openid: options.popenid,
                id: options.gameid
              },
              success(res) {
                var date1 = new Date();
                var contest = res.data.contest;
                var contact = contest.contact;
                contact = contact.substring(0, 3) + "****" + contact.substring(7);
                var reg = new RegExp("-", "g");
                var newTime = contest.postdate.replace(reg, "/");
                var date = new Date(newTime);
                var seconds = 0;
                var minutes = 0;
                var hour = 0;
                var day = 0;
                var month = 0;
                var year = 0;
                var timeDifference = "";
                if ((date1 - date) / 1000 >= 60) {
                  if ((date1 - date) / 1000 / 60 >= 60) {
                    if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                      if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                        if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                          year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                          timeDifference = year + "年前";
                        } else {
                          month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                          timeDifference = month + "个月前";
                        }
                      } else {
                        day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                        timeDifference = day + "天前";
                      }
                    } else {
                      hour = parseInt((date1 - date) / 1000 / 60 / 60);
                      timeDifference = hour + "小时前";
                    }
                  } else {
                    minutes = parseInt((date1 - date) / 1000 / 60);
                    timeDifference = minutes + "分钟前";
                  }
                } else {
                  seconds = parseInt((date1 - date) / 1000);
                  timeDifference = seconds + "秒前";
                };
                count++;
                if (count == all) {
                  wx.hideLoading();
                }
                that.setData({
                  timeDifference: timeDifference,
                  data: contest,
                  contact: contact,
                });
              }
            })
            if (database.userId != "") {
              count++;
              if (count == all) {
                wx.hideLoading();
              }
              that.setData({
                openid: database.userId,
              })
            } else {
              wx.login({
                success(res) {
                  database.code = res.code;
                  wx.request({
                    url: database.url + '/api/V2/integral/integral_openid',
                    method: "POST",
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                      code: database.code,
                      accessToken: database.accessToken,
                      weChatId: "woaizuqiuyuezhanguangchang",
                      regionName: "other"
                    },
                    success(res) {
                      var sessoionKey = res.data.data.session_key;
                      wx.getUserInfo({
                        data: {
                          withCredentials: true,
                          lang: "zh_CN"
                        },
                        success(res) {
                          wx.request({
                            url: database.url + '/api/V2/user/we_chat_user',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              weChatId: "woaizuqiuyuezhanguangchang",
                              encryptedData: res.encryptedData,
                              iv: res.iv,
                              sessionKey: sessoionKey,
                              weChatName: "我爱足球约战广场"
                            },
                            success(res) {
                              database.unionid = res.data.data.unionId;
                              wx.request({
                                url: database.url1 + '/api/V2/user/verify_wechat_small',
                                method: "POST",
                                header: {
                                  'content-type': 'application/x-www-form-urlencoded'
                                },
                                data: {
                                  openid: database.unionid,
                                  accessToken: database.accessToken
                                },
                                success(res) {
                                  count++;
                                  if (count == all) {
                                    wx.hideLoading();
                                  }
                                  if (res.data.data.verify == "no") {
                                    database.login = false;
                                    that.setData({
                                      openid: ""
                                    })
                                  } else {
                                    database.userId = res.data.data.id;
                                    database.login = true;
                                    that.setData({
                                      openid: database.userId
                                    })
                                  }
                                }
                              })
                            }
                          })
                        }
                      })
                    }
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  bindTeamChange: function (e) {
    var that = this;
    this.setData({
      team_index: e.detail.value
    })
    var teamcount = e.currentTarget.dataset.teamcount;
    if (teamcount != 0) {
      var teamid = this.data.teamList[this.data.team_index].id;
      if (teamid != null && teamid != '') {
        wx.request({
          url: database.url + '/project/uploadYuezhan.action',
          header: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          data: {
            matchid: e.currentTarget.dataset.gameid,
            openid: e.currentTarget.dataset.openid,
            teamid: teamid
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success(res) {
            that.setData({
              flag: true,
              flag1: true,
              flag2: false
            });
          }
        })
      }
    } else {
      that.setData({
        flag: true,
        flag1: false,
        flag2: true
      });
    }
  },
  apply: function (e) {
    console.log(e)
    var that = this;
    if (database.login) {
      wx.request({
        url: database.url1 + '/api/V2/softball/softball_list',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          userId: database.userId
        },
        success(res) {
          if (res.data.data.softballList.length != 0) {
            var teamList = res.data.data.softballList;
            var team = [];
            for (var i = 0; i < teamList.length; i++) {
              team[i] = teamList[i].title
            }
            that.setData({
              team: team,
              teamList: teamList,
              flag3: false,
              team_index: e.detail.value,
              form_id: e.detail.formId
            })
          } else {
            that.setData({
              flag: true,
              flag1: false,
              flag2: true
            })
          }
        }
      })
    } else {
      that.setData({
        flag: false,
        flag1: true,
        flag2: true
      });
    }
  },
  topublisher: function (e) {
    var gameid = e.currentTarget.dataset.gameid;
    var popenid = e.currentTarget.dataset.popenid;
    wx.redirectTo({
      url: '/pages/fight_detail/publisher/publisher?gameid=' + gameid + '&popenid=' + popenid,
    })
  },
  binding: function () {
    wx.redirectTo({
      url: '/pages/newBinding/newBinding',
    })
  },
  cancel: function () {
    this.setData({
      flag: true,
      flag1: true,
      flag2: true
    })
  },
  create: function () {
    wx.redirectTo({
      url: '/pages/my/myteam/createteam/createteam',
    })
  },
  showLocation: function () {
    var that = this;
    wx.openLocation({
      latitude: parseFloat(that.data.data.lat),
      longitude: parseFloat(that.data.data.lng),
    })
  },
  toTeamDetail: function (e) {
    var teamid = e.currentTarget.dataset.teamid;
    var userid = e.currentTarget.dataset.userid;
    wx.redirectTo({
      url: '/pages/my/myteam/mainpage1/mainpage1?id=' + teamid + '&userid=' + userid,
    })
  },
  toHome: function () {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.data.pteamname + '发布了约战，赶快去应战吧',
      path: '/pages/fight_detail/battle/battle?openid=' + that.data.share_openid + '&gameid=' + that.data.share_id,
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})